import { codeLogin } from '../../../api/index'
const app = getApp()
import { setCache } from '../../../utils/util'
Page({
  data: {
    canuse: false,

    smsCode: '',
    mobile: '',

    loginMark: false,
  },
  
  welogin() {
    if (this.data.loginMark) {
      return
    }
    this.data.loginMark = true
    setTimeout(() => {
      this.data.loginMark = false
    }, 1000);
   
    codeLogin({ code: this.data.code }).then((res) => {
      setCache('x_tk', res.datas.TOKEN)
      setCache('isLogin', true)
      setCache('userInfo', res.datas)
      app.globalData.isLogin = true
      app.globalData.x_tk = res.datas.TOKEN
      app.globalData.isReload = true
      app.globalData.userInfo = res.datas
      wx.navigateBack()
      this.data.loginMark = false
      
    })
    this.setData({ showPhone: true })
  },
  cdlogin() {
    if (this.data.loginMark) {
      return
    }
    this.data.loginMark = true
    setTimeout(() => {
      this.data.loginMark = false
    }, 1000);
   
    codeLogin({ code: this.data.code }).then((res) => {
      
      setCache('x_tk', res.datas.TOKEN)
      setCache('isLogin', true)
      setCache('userInfo', res.datas)
      app.globalData.isLogin = true
      app.globalData.x_tk = res.datas.TOKEN
      app.globalData.isReload = true
      app.globalData.userInfo = res.datas
      wx.navigateBack()
      this.data.loginMark = false
      if (!res.datas.avatar || res.datas.avatar.includes('default_avatar.png')) {
        wx.getUserProfile({
          desc: "获取你的昵称、头像123",
          success: res => {
            const p = {
              nickname: res.userInfo.nickName,
              avatar: res.userInfo.avatarUrl,
            }
            setProfile(p)
            wx.showToast({
              icon: 'none',
              title: '请授权获取手机号'
            })
            this.setData({ showPhone: true })
          },
        })
      } else {
        if (!res.datas.mobile) {
          wx.showToast({
            icon: 'none',
            title: '请授权获取手机号'
          })
          this.setData({ showPhone: true })
        } else {
          wx.navigateBack()
        }
      }
    })
  },
  onLoad: function (e) {
    if (wx.getUserProfile) {
      this.setData({ canuse: true })
    }
    this.getCode()
    this.data.intnum = setInterval(() => {
      this.getCode()
    }, 5 * 60 * 1000);
  },
  getCode() {
    wx.login({
      success: (res) => {
        if (res.code) {
          this.data.code = res.code
        }
      }
    })
  },
  
  onShow: function () {
  },
  smsInput(e){
    this.setData({
      smsCode: e.detail.value
    })
  },
  namInput(e) {
    this.setData({
      mobile: e.detail.value
    })
  },
  pwdlogin() {
    let msg = ''
    if (!this.data.smsCode) {
      msg = '请输入密码'
    }
    if (!this.data.mobile) {
      msg = '请输入手机号'
    }
    if (msg) {
      wx.showToast({
        icon: 'none',
        title: msg,
        duration:2000,
      })
      return
    }
    wx.login({
      success: (res) => {
        const p = {
          user: this.data.mobile,
          pwd: this.data.smsCode,
          code: res.code
        }
        codeLogin(p).then((res) => {
          setCache('x_tk', res.datas.api_token)
          setCache('isLogin', true)
          setCache('userInfo', res.datas)
          app.globalData.isLogin = true
          app.globalData.x_tk = res.datas.api_token
          app.globalData.isReload = true
          app.globalData.userInfo = res.datas
          wx.navigateBack()
        })
      }
    })
  },
  getPhoneNumber(e) {
    if (e.detail.errMsg.includes('ok')) {
      wx.login({
        success: (res) => {
          if (res.code) {
            const p = {
              code: res.code,
              encryptedData: e.detail.encryptedData,
              iv: e.detail.iv
            }
            getMobile(p).then((res2) => {
              const p2 = {
                mobile: res2.datas.phoneNumber
              }
              setProfile(p2)
              wx.navigateBack()
            })
          }
        }
      })
    }
  },

  bindgetuserinfo(e) {
console.log('========获取用户信息xxxx=========');
console.log(e);
console.log('========获取用户信息xxxxx=========');

    if (e.detail.errMsg.includes('ok')) {
      const p = {
        code: this.data.code,
        iv: e.detail.iv,
        encryptedData: e.detail.encryptedData
      }
      getMobile(p).then((res2) => {
        this.setData({ avatar: res2.datas.avatarUrl, uname: res2.datas.nickName })
        this.getCode()
        let p2 = {
          avatar: res2.datas.avatarUrl,
          name: res2.datas.nickName,
          nickname: res2.datas.nickName,
        }
        wx.showToast({
          icon: 'none',
          title: '请授权获取手机号'
        })
        setProfile(p2)
      })
    }
  },
  getuserInfo() {
    console.log('========获取用户信息=========');

console.log('========获取用户信息=========');
    if (!this.data.canuse) {
      return
    }
    this.cdlogin()
  },
  
  onUnload(){
    // // this.data.timer && clearInterval(this.data.timer)
    // this.data.intnum && clearInterval(this.data.intnum)
  },

})